import java.io.*;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author chen
 */
public class Craw extends Thread {

    static Pattern compile = Pattern.compile("<img[\\s\\S]*?src=\"(?<url>.*?)\"[\\s\\S]+?>");
    static ArrayList<String> imgType = new ArrayList() {{
        add("jpg");
        add("jpeg");
        add("png");
        add("bmp");
        add("tiff");
    }};
    List<String> missions = new ArrayList<>();

    public static void main(String[] args) throws IOException {
        long t0 = System.currentTimeMillis();
        List<String> imgUrls = getMatchString("zhihu.html", "utf-8");

        for (String imgUrl : imgUrls) {
            System.out.println(imgUrl);
            savePic(imgUrl);
        }
        long t1 = System.currentTimeMillis();
        System.out.printf("time spend: %d\n", t1 - t0);

    }

    public static List<String> getMatchString(String fileName, String encoding) throws IOException {
        List<String> pics = new ArrayList<>();
        File file = new File(fileName);
        long filelength = file.length();
        byte[] filecontent = new byte[(int) filelength];
        FileInputStream in = new FileInputStream(file);
        in.read(filecontent);
        in.close();
        String data = new String(filecontent, encoding);
        Matcher matcher = compile.matcher(data);
        while (matcher.find()) {
            String img = matcher.group("url");
            pics.add(img);
        }
        return pics;
    }

    public static void savePic(String site) {
        URL url;
        try {
            url = new URL(site);

            DataInputStream dataInputStream = new DataInputStream(url.openStream());

            String path = "./pic/" + site.split("/")[site.split("/").length - 1];
            String suffix = site.split("\\?|\\.|/|\\|\\*|<|>|\\|")[site.split("\\?|\\.|/|\\|\\*|<|>|\\|").length - 1];
            if (!imgType.contains(suffix)) {
                path = "./pic/" + suffix + ".jpg";
            }
            System.out.println(path);
            FileOutputStream fileOutputStream = new FileOutputStream(new File(path));
            ByteArrayOutputStream output = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int length;
            while ((length = dataInputStream.read(buffer)) > 0) {
                output.write(buffer, 0, length);
            }
            fileOutputStream.write(output.toByteArray());
            dataInputStream.close();
            fileOutputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void run() {
        for (String i : missions) {
            savePic(i);
        }
    }


}
